package com.moli.iov.handle.alarm.enums;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  报警编码枚举
 * </p>
 *
 * @author: zql
 * @Date: 2019/2/18 11:14
 */
public enum AlarmCodeEnum {

    /**
     * 温度差异报警
     */
    TEMPERATURE_DIFFERENCE(0, "温度差异报警", 1),

    /**
     * 电池高温报警
     */
    BATTERY_HIGH_TEMPERATURE(1, "电池高温报警", 2),

    /**
     * 车载储能装置类型过压报警
     */
    ENERGY_STORING_DEVICE_OVERPRESSURE(2, "车载储能装置类型过压报警",2),

    /**
     * 车载储能装置类型欠压报警
     */
    ENERGY_STORING_DEVICE_UNDERPRESSURE(3,"车载储能装置类型欠压报警",2 ),

    /**
     * SOC低报警  终端上报
     */
    SOC_LOW(4, "SOC低报警",2),

    /**
     * 单体电池过压报警
     */
    SINGLE_BATTERY_OVERPRESSURE(5, "单体电池过压报警",2),

    /**
     * 单体电池欠压报警
     */
    SINGLE_BATTERY_UNDERPRESSURE(6, "单体电池欠压报警",2),

    /**
     * SOC过高报警
     */
    SOC_OVER(7, "SOC过高报警",  2),

    /**
     * SOC跳变报警
     */
    SOC_LEAPING(8, "SOC跳变报警",  2),

    /**
     * 可充电储能系统不匹配报警
     */
    STORAGE_SYSTEMS_NON_MATCHED(9, "可充电储能系统不匹配报警",   1),

    /**
     * 电池单体一致性差报警
     */
    BATTERY_SINGLE_CONFORMING_BAD(10, "电池单体一致性差报警",   1),

    /**
     * 绝缘报警
     */
    INSULATION(11, "绝缘报警",   2),

    /**
     * DC-DC温度报警
     */
    DCDC_TEMPERATURE(12, "DC-DC温度报警",   2),

    /**
     * 制动系统报警
     */
    BRAKE_SYSTEM(13, "制动系统报警",   3),

    /**
     * DC-DC状态报警
     */
    DCDC_STATE(14, "DC-DC状态报警",   3),

    /**
     * 驱动电机控制器温度报警
     */
    DRIVE_MOTOR_CONTROL_TEMPERATURE(15, "驱动电机控制器温度报警",   2),

    /**
     * 高压互锁状态报警
     */
    HIGH_PRESSURE_INTERLOCK_STATE(16, "高压互锁状态报警",   2),

    /**
     * 驱动电机温度报警
     */
    DRIVE_MOTOR_TEMPERATURE(17, "驱动电机温度报警",   2),

    /**
     * 车载储能装置类型过充报警
     */
    ENERGY_STORING_DEVICE_OVERCHARGE(18, "车载储能装置类型过充报警",   1),

    /**
     * 可充电储能装置故障报警
     */
    STORING_DEVICE(19, "可充电储能装置故障", 3),

    /**
     * 驱动电机故障编报警
     */
    DRIVE_MOTOR(20, "驱动电机故障", 3),

    /**
     * 蓄电池低电压报警
     */
    BATTERY_VOLTAGE(21, "蓄电池低电压报警", 2),

    /**
     * 失联车辆报警
     */
    OFF_LINE(22, "失联车辆报警", 3),

    /**
     * 缺失车牌报警
     */
    @Deprecated LOSE(23, "缺失车牌报警", 1),

    /**
     * SOC低电压报警  平台判断 低于20%
     */
    SOC(24, "SOC电压低报警", 2),

    /**
     * 出区域报警
     */
    OUT_FENCE(25, "出区域报警", 2),

    /**
     * 进区域报警
     */
    IN_FENCE(26, "进区域报警", 2),

    /**
     * 里程异变报警
     */
    MILEAGE_EXCEPTION_CHANGE(27, "里程异变报警", 3),

    /**
     * 超里程报警
     */
    @Deprecated
    OVERMILE(28, "超里程报警", 3),

    /**
     * 设备变更报警
     */
    DEVICE_CHAGNGE(29, "设备更换", 3),

    /**
     * 设备拆除报警
     */
    DEVICE_TEAR_DOWN(30, "设备拆除", 3),

    /**
     * 未授权车辆注册报警
     */
    UNAUTHORIZED_VEHICLE_REGISTRATION(31, "未授权车辆注册", 3);

    private int flag;

    private int level;

    private String msg;

    AlarmCodeEnum(int flag, String msg, int level){
        this.flag = flag;
        this.msg = msg;
        this.level = level;
    }

    public static String getMsg(int flag){
        for(AlarmCodeEnum e : AlarmCodeEnum.values()){
            if(e.flag == flag){
                return e.msg;
            }
        }
        return "";
    }

    public static int getLevel(int flag){
        for(AlarmCodeEnum e : AlarmCodeEnum.values()){
            if(e.flag == flag){
                return e.level;
            }
        }
        return -1;
    }

    public static AlarmCodeEnum getEnum(int flag){
        for(AlarmCodeEnum e : AlarmCodeEnum.values()){
            if(e.flag == flag){
                return e;
            }
        }
        return null;
    }

    public static List<AlarmCodeEnum> getEnumList(){
        List<AlarmCodeEnum> list = new ArrayList<>(Arrays.asList(AlarmCodeEnum.values()));
        list.remove(OVERMILE);
        list.remove(LOSE);
        return list;
    }

    /**
     * 车载终端列表
     * @return
     */
    public static List<Integer> getTerminalAlarms(){
        return Arrays.asList(
                TEMPERATURE_DIFFERENCE.getFlag(),
                BATTERY_HIGH_TEMPERATURE.getFlag(),
                ENERGY_STORING_DEVICE_OVERPRESSURE.getFlag(),
                ENERGY_STORING_DEVICE_UNDERPRESSURE.getFlag(),
                SOC_LOW.getFlag(),
                SINGLE_BATTERY_OVERPRESSURE.getFlag(),
                SINGLE_BATTERY_UNDERPRESSURE.getFlag(),
                SOC_OVER.getFlag(),
                SOC_LEAPING.getFlag(),
                STORAGE_SYSTEMS_NON_MATCHED.getFlag(),
                BATTERY_SINGLE_CONFORMING_BAD.getFlag(),
                INSULATION.getFlag(),
                DCDC_TEMPERATURE.getFlag(),
                BRAKE_SYSTEM.getFlag(),
                DCDC_STATE.getFlag(),
                DRIVE_MOTOR_CONTROL_TEMPERATURE.getFlag(),
                HIGH_PRESSURE_INTERLOCK_STATE.getFlag(),
                DRIVE_MOTOR_TEMPERATURE.getFlag(),
                ENERGY_STORING_DEVICE_OVERCHARGE.getFlag(),
                STORING_DEVICE.getFlag(),
                DRIVE_MOTOR.getFlag(),
                BATTERY_VOLTAGE.getFlag(),
                SOC.getFlag()
              //  MILEAGE_EXCEPTION_CHANGE.getFlag(),
              //  DEVICE_CHAGNGE.getFlag()
              );
    }

    /**
     * 判断报警是否可以忽略
     * @param flag
     * @return
     */
    public static boolean isIgnore(int flag){
        List<Integer> ignores = Arrays.asList(
            OFF_LINE.getFlag(), OUT_FENCE.getFlag(), IN_FENCE.getFlag());
        return ignores.contains(flag);
    }

    /**
     * 是否离线报警
     * @param flag
     * @return
     */
    public static boolean isOffLine(int flag){
        return OFF_LINE.getFlag() == flag;
    }

    /**
     * 是否车载终端报警
     * @param flag
     * @return
     */
    public static boolean isTerminal(int flag){
        List<Integer> terminalAlarms = getTerminalAlarms();
        return terminalAlarms.contains(flag);
    }

    /**
     * 是否围栏报警
     * @param flag
     * @return
     */
    public static boolean isFence(int flag){
        return OUT_FENCE.getFlag() == flag || IN_FENCE.getFlag() == flag;
    }

    /**
     * 是否里程异变报警
     * @param flag
     * @return
     */
    public static boolean isMileExcep(int flag){
        return MILEAGE_EXCEPTION_CHANGE.getFlag() == flag;
    }

    public int getFlag() {
        return flag;
    }

    public int getLevel() {
        return level;
    }


    public String getMsg() {
        return msg;
    }
}
